<?php
class Celular extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'celular';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nuevo, empresa, idModelo', 'required'),
			array('nuevo, empresa, idModelo, camara, radioFm, qwerty, touch, bluetooth, wifi, gps, accesorios', 'numerical', 'integerOnly'=>true),
			array('mp', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nuevo, empresa, idModelo, camara, mp, radioFm, qwerty, touch, bluetooth, wifi, gps, accesorios', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idModelo0' => array(self::BELONGS_TO, 'Modelo', 'idModelo'),
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nuevo' => 'Estado',
			'empresa' => 'Empresa',
			'idModelo' => 'Modelo',
			'camara' => 'Camara',
			'mp' => 'Mega Pixeles',
			'radioFm' => 'Radio Fm',
			'qwerty' => 'Teclado Qwerty',
			'touch' => 'Pantalla Tactil',
			'bluetooth' => 'Bluetooth',
			'wifi' => 'Wifi',
			'gps' => 'Gps',
			'accesorios' => 'Contiene Accesorios',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nuevo',$this->nuevo);
		$criteria->compare('empresa',$this->empresa);
		$criteria->compare('idModelo',$this->idModelo);
		$criteria->compare('camara',$this->camara);
		$criteria->compare('mp',$this->mp);
		$criteria->compare('radioFm',$this->radioFm);
		$criteria->compare('qwerty',$this->qwerty);
		$criteria->compare('touch',$this->touch);
		$criteria->compare('bluetooth',$this->bluetooth);
		$criteria->compare('wifi',$this->wifi);
		$criteria->compare('gps',$this->gps);
		$criteria->compare('accesorios',$this->accesorios);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getNuevo(){
		return array(
			'0'=>'Usado',
			'1'=>'Nuevo',
		);
	}
	
	public function getEmpresas(){
		return array(
			'0'=>'Liberado',
			'1'=>'Claro',
			'2'=>'Movistar',
			'3'=>'Personal',
			'4'=>'Otra',
		);
	}
	
	public function getEmpresa(){
		foreach($this->Empresas as $id=>$empresa)
			if($id==$this->empresa)
				return $empresa;
	}
	
	public function getModelo(){
		return Modelo::model()->findByPk($this->idModelo)->Nombre;
	}
	
	public function getModeloYMarca(){
		return Modelo::model()->findByPk($this->idModelo)->ModeloYMarca;
	}
	
	public function getEstado(){
		if($this->nuevo)
			return 'Nuevo';
		else
			return 'Usado';
	}
	
	public function Opcion($op){
		if($op)
			return 'Si';
		else
			return 'No';
	}
	
	public function getMarca(){
		return Modelo::model()->findByPk($this->idModelo)->Marca;
	}
}